﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using MetroFramework.Forms;
using System.IO;
using ComLib.ComType;
using IPCPages;

namespace FormPageConfigs
{
    public partial class FormAimingSetting : MetroForm
    {
        private bool _bInited = false;
      
        private AimingOption _option = new AimingOption();
        public AimingOption Option
        {
            get { return _option; }
            set { _option = value; }
        }

        public FormAimingSetting()
        {
            LoadConfig();
            InitializeComponent();
        }

        private void LoadConfig()
        {
            if (!File.Exists(_option.ConfigFilePath))
            {
                _option.Save();
            }
            _option.Load();
        }

        private void InitCtrls()
        {
            numericUpDownCamHeight.Value = (decimal)_option.CamHeight;
            numericUpSenorHeight.Value = (decimal)_option.SensorHeight;
            numericUpDownMaxDistance.Value = (decimal)_option.MaxMovingDistance;
            numericUpDownFormTop.Value = (decimal)_option.EyesFormHeadtopHeight;
            numericUpDownStepsCount.Value = (decimal)_option.TotalStepsCount;
            numericUpDownMaximumError.Value = (decimal)_option.MaximumError;
            numericUpDownConMin.Value = (decimal)_option.ConfidenceRangle.Min;
            numericUpDownConMax.Value = (decimal)_option.ConfidenceRangle.Max;
            numericUpDownMostConMin.Value = (decimal)_option.MostConfidentRangle.Min;
            numericUpDownMostConMax.Value = (decimal)_option.MostConfidentRangle.Max;
        }

        private void UpdateData()
        {
            _option.CamHeight = (double)numericUpDownCamHeight.Value;
            _option.SensorHeight = (double)numericUpSenorHeight.Value;
            _option.MaxMovingDistance = (double)numericUpDownMaxDistance.Value;
            _option.EyesFormHeadtopHeight = (double)numericUpDownFormTop.Value;
            _option.TotalStepsCount = (int)numericUpDownStepsCount.Value;
            _option.MaximumError = (int)numericUpDownMaximumError.Value;
            int Min = (int)numericUpDownConMin.Value;
            int Max = (int)numericUpDownConMax.Value;
            _option.ConfidenceRangle = new IntRangle(Min, Max);
            int MostMin = (int)numericUpDownMostConMin.Value;
            int MostMax = (int)numericUpDownMostConMax.Value;
            _option.MostConfidentRangle = new IntRangle(Min, Max);
            _option.Save();
        }

        private void buttonCancell_Click(object sender, EventArgs e)
        {
            this.DialogResult = System.Windows.Forms.DialogResult.Cancel;
            this.Close();
        }

        private void buttonOK_Click(object sender, EventArgs e)
        {
            UpdateData();
            this.DialogResult = System.Windows.Forms.DialogResult.Cancel;
            this.Close();
        }

        private void FormAimingSetting_Load(object sender, EventArgs e)
        {
            InitCtrls();
            _bInited = true;
        }
    }
}
